package com.example.music;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.example.music.Bean.LocalMusicBean;
import com.example.music.DB.SongInfo;
import com.example.music.DB.UserInfo;
import com.example.music.Utils.MusicPlaylist;

import org.litepal.LitePal;
import org.litepal.crud.LitePalSupport;
import org.litepal.tablemanager.Connector;
import org.w3c.dom.Text;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import java.util.Timer;
import java.util.TimerTask;

public class StartActivity extends AppCompatActivity {
    MusicPlaylist mDatas;                       //    数据源
    private Button btn_start;
    boolean flag = false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Connector.getDatabase();
        setContentView(R.layout.activity_start);
        SharedPreference();     //初始化sharedPreference
        btn_start = findViewById(R.id.start_journey);
        btn_start.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                flag = true;
                Intent intent = new Intent(StartActivity.this, HomeActivity.class);
                startActivity(intent);
                finish();
            }
        });

        mDatas = MusicPlaylist.getInstance();   //首页歌曲列表
        mDatas.updatePlaylist(this);    //更新本地资源
        /*************初始化数据库**************/
        //        LitePal.deleteAll(SongInfo.class);
//        for(LocalMusicBean item:mDatas.getSongs()){
//            SongInfo song = new SongInfo(item.getSong(),item.getSinger(),"",item.getPath(), item.getAlbumArt());
//            song.save();
//        }
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                if(!flag) {
                    Intent intent = new Intent(StartActivity.this, HomeActivity.class);
                    startActivity(intent);
                    finish();
                }
            }
        }, 3000);
    }
    public void SharedPreference(){
        SharedPreferences sharedPreferences = getSharedPreferences("my_prefs", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putBoolean("isLogin", false);
        editor.putString("curUserName", "未登录");
        editor.putInt("curUserId", -1);
        editor.putInt("curSongId", -1);
        editor.commit();    // 同步保存数据
    }
}